package cn.wjh.business.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.ruoyi.common.utils.DateUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import cn.wjh.business.domain.Competition;
import cn.wjh.business.service.ICompetitionService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 比赛管理Controller
 *
 * @author jiraWu
 * @date 2023-09-16
 */
@RestController
@RequestMapping("/business/competition")
public class CompetitionController extends BaseController {
    @Resource
    private ICompetitionService competitionService;

/**
 * 查询比赛管理列表
 */
@PreAuthorize("@ss.hasPermi('business:competition:list')")
@GetMapping("/list")
    public TableDataInfo list(Competition competition) {
        startPage();
        List<Competition> list = competitionService.list(new QueryWrapper<>(competition));
        return getDataTable(list);
    }

    /**
     * 导出比赛管理列表
     */
    @PreAuthorize("@ss.hasPermi('business:competition:export')")
    @Log(title = "比赛管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Competition competition) {
        List<Competition> list = competitionService.list(new QueryWrapper<>(competition));
        ExcelUtil<Competition> util = new ExcelUtil<Competition>(Competition. class);
        util.exportExcel(response, list, "比赛管理数据");
    }

    /**
     * 获取比赛管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:competition:query')")
    @GetMapping(value = "/{competingId}")
    public AjaxResult getInfo(@PathVariable("competingId") Long competingId) {
        return AjaxResult.success(competitionService.getById(competingId));
    }

    /**
     * 新增比赛管理
     */
    @PreAuthorize("@ss.hasPermi('business:competition:add')")
    @Log(title = "比赛管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Competition competition) {
        competition.setCreateBy(getUsername());
        competition.setCreateTime(DateUtils.getNowDate());
        return toAjax(competitionService.save(competition));
    }

    /**
     * 修改比赛管理
     */
    @PreAuthorize("@ss.hasPermi('business:competition:edit')")
    @Log(title = "比赛管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Competition competition) {
        competition.setUpdateBy(getUsername());
        competition.setUpdateTime(DateUtils.getNowDate());
        return toAjax(competitionService.updateById(competition));
    }

    /**
     * 删除比赛管理
     */
    @PreAuthorize("@ss.hasPermi('business:competition:remove')")
    @Log(title = "比赛管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{competingIds}")
    public AjaxResult remove(@PathVariable List<Long> competingIds) {
        return toAjax(competitionService.removeByIds(competingIds));
    }
}

